package com.teamb6.shop.service.impl;

import com.teamb6.shop.domain.Activity;
import com.teamb6.shop.domain.ActivityExample;
import com.teamb6.shop.mapper.ActivityMapper;
import com.teamb6.shop.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired(required = false)
    private ActivityMapper activityMapper;

    @Override
    public Activity findActivity(Integer id) {
        Activity activity = activityMapper.selectByPrimaryKey(id);
        return activity;
    }

    @Override
    public int saveActivity(Activity activity) {
        return activityMapper.insertSelective(activity);


    }

    //分页查询
    @Override
    public List<Activity> listActivity(String keyword) {
        keyword = "%" + keyword + "%";
        ActivityExample activityExample = new ActivityExample();
        activityExample.or().andActivitynameLike(keyword);
        List<Activity> activities = activityMapper.selectByExample(activityExample);
        return activities;
    }

    //删除活动
    @Override
    public int delectActivity(Integer id) {
        return activityMapper.deleteByPrimaryKey(id);
    }

    //  批量删除
    @Override
    public int multiDel(String id) {
        ActivityExample activityExample = new ActivityExample();
        List<String> list = Arrays.asList(id.split(","));
        List<Integer> ilist = list.stream().map(Integer::parseInt).collect(Collectors.toList());
        activityExample.or().andActivityidIn(ilist);
        return activityMapper.deleteByExample(activityExample);
    }

    //修改活动
    @Override
    public int editActivity(Activity activity) {
        return activityMapper.updateByPrimaryKeySelective(activity);
    }

    @Override
    public List<Activity> getAllActivity(ActivityExample activityExample) {
        return activityMapper.selectByExample(activityExample);
    }

    @Override
    public Activity selectByKey(Integer activityid) {
        return activityMapper.selectByPrimaryKey(activityid);
    }
}

